from django.db import models


# 用户基本信息表（手机号即为唯一登录账号）
class User(models.Model):
    id = models.BigAutoField(primary_key=True)  # 主键
    phone = models.CharField(max_length=20, unique=True)  # 手机号，唯一
    real_name = models.CharField(max_length=50)  # 真实姓名
    id_card = models.CharField(max_length=20, null=True)  # 身份证号
    id_img_url = models.CharField(max_length=255, null=True)
    status = models.SmallIntegerField(default=0)  # 0-未认证,1-已认证,2-禁用
    register_time = models.DateTimeField(auto_now_add=True)  # 注册时间
    avatar = models.CharField(max_length=255, blank=True, null=True)  # 头像
    type = models.SmallIntegerField(null=True)  # 0-乘客,1-司机
    created_at = models.DateTimeField(auto_now_add=True)  # 创建时间
    updated_at = models.DateTimeField(auto_now=True)  # 更新时间

    class Meta:
        db_table = 'users'
        verbose_name = '用户基本信息'
        verbose_name_plural = verbose_name


# 用户地址表
class UserAddress(models.Model):
    id = models.BigAutoField(primary_key=True)  # 主键
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE)  # 外键，关联用户表
    address_type = models.SmallIntegerField(null=True)  # 0-家,1-公司,2-其他
    phone = models.CharField(max_length=20, null=True)  # 手机号
    province = models.CharField(max_length=50, null=True)  # 省份
    city = models.CharField(max_length=50, null=True)  # 城市
    district = models.CharField(max_length=50, null=True)  # 区
    detail_address = models.CharField(max_length=255, null=True)  # 详细地址
    is_default = models.SmallIntegerField(default=0)  # 0-否,1-是 默认地址
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user_addresses'
        verbose_name = '用户地址'
        verbose_name_plural = verbose_name


# 用户登录日志表
class UserLoginLog(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE)
    login_time = models.DateTimeField()
    device_type = models.CharField(max_length=50, blank=True, null=True)
    login_status = models.SmallIntegerField()  # 0-失败,1-成功
    created_at = models.DateTimeField()

    class Meta:
        db_table = 'user_login_logs'
        verbose_name = '用户登录日志'
        verbose_name_plural = verbose_name


# 人脸识别记录表
class FaceRecognition(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE)
    face_image_url = models.CharField(max_length=255)
    recognition_time = models.DateTimeField()
    recognition_result = models.SmallIntegerField()  # 0-失败,1-成功
    similarity_score = models.DecimalField(max_digits=6, decimal_places=2, blank=True, null=True)
    created_at = models.DateTimeField()
    updated_at = models.DateTimeField()

    class Meta:
        db_table = 'face_recognitions'
        verbose_name = '人脸识别记录'
        verbose_name_plural = verbose_name


# 司机认证历史表
class UserAuthHistory(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE)
    auth_type = models.SmallIntegerField()  # 0--人脸识别
    auth_status = models.SmallIntegerField()  # 0-未通过,1-通过
    auth_time = models.DateTimeField()
    auditor_id = models.BigIntegerField(blank=True, null=True)
    remark = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user_auth_histories'
        verbose_name = '司机认证历史'
        verbose_name_plural = verbose_name


# 司机接单偏好设置表
class UserPreference(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE)
    accept_order_type = models.CharField(max_length=50, blank=True, null=True)
    preferred_area = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user_preferences'
        verbose_name = '司机接单偏好设置'
        verbose_name_plural = verbose_name


# 用户黑名单表
class UserBlacklist(models.Model):
    id = models.BigAutoField(primary_key=True)  # 主键
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE,
                             related_name='blacklist_user')  # 外键，关联用户表，删除时级联删除
    target_user = models.ForeignKey(User, db_column='target_user_id', on_delete=models.CASCADE,
                                    related_name='blacklisted_by')  # 外键，关联用户表，删除时级联删除
    black_type = models.SmallIntegerField()  # 0-乘客,1-司机
    reason = models.CharField(max_length=255, blank=True, null=True)  # 黑名单原因，可以为空
    created_at = models.DateTimeField(auto_now_add=True)  # 创建时间
    updated_at = models.DateTimeField(auto_now=True)  # 更新时间

    class Meta:
        db_table = 'user_blacklists'
        verbose_name = '用户黑名单'
        verbose_name_plural = verbose_name


# 用户第三方账号绑定表
class UserSocialAccount(models.Model):
    id = models.BigAutoField(primary_key=True)
    user = models.ForeignKey(User, db_column='user_id', on_delete=models.CASCADE)
    platform = models.CharField(max_length=20)
    openid = models.CharField(max_length=100)
    token = models.CharField(max_length=255, blank=True, null=True)
    created_at = models.DateTimeField(auto_now_add=True)
    updated_at = models.DateTimeField(auto_now=True)

    class Meta:
        db_table = 'user_social_accounts'
        verbose_name = '用户第三方账号绑定'
        verbose_name_plural = verbose_name
